/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.iecg.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iecg.api.entity.IecgUser;
import com.iecg.service.IecgUserService;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 日志表
 *
 * @author pigx code generator
 * @date 2019-08-21 10:03:00
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user" )
@Api(value = "user", tags = "用户信息")
public class IecgUserController {

    private final IecgUserService userService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param user 用户管理表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getUserPage(Page page, IecgUser user) {
        return R.ok(userService.page(page, Wrappers.query(user)));
    }


    /**
     * 通过id查询日志表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(userService.getById(id));
    }

    /**
     * 新增日志表
     * @param user 日志表
     * @return R
     */
    @ApiOperation(value = "新增用户表", notes = "新增用户表")
    @SysLog("新增用户表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('iecg_user_add')" )
    public R save(@RequestBody IecgUser user) {
        return R.ok(userService.save(user));
    }

    /**
     * 修改用户表
     * @param user 用户表
     * @return R
     */
    @ApiOperation(value = "修改用户表", notes = "修改用户表")
    @SysLog("修改用户表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('iecg_user_edit')" )
    public R updateById(@RequestBody IecgUser user) {
        return R.ok(userService.updateById(user));
    }

    /**
     * 通过id删除用户表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除用户表", notes = "通过id删除用户表")
    @SysLog("通过id删除用户表" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('iecg_user_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(userService.removeById(id));
    }


	/**
	 * 获取指定用户全部信息
	 *
	 * @return 用户信息
	 */
	@Inner
	@GetMapping("/info/{username}")
	public R info(@PathVariable String username) {
		IecgUser user = userService.getOne(Wrappers.<IecgUser>query()
				.lambda().eq(IecgUser::getName, username));
		if (user == null) {
			return R.failed(null, String.format("用户信息为空 %s", username));
		}
		return R.ok(user);
	}

}
